﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of ReadOnlyContactDetailsCtrl class
	/// Display of contact details
	/// </summary>
	public partial class ReadOnlyContactDetailsCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Constants and enumerations

		private const int PointInsertRowIndex = 2;

		/////////////////////////////////////////////////////////////
		// Construction

		public ReadOnlyContactDetailsCtrl()
		{

			// Load xaml resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Event handlers

		private void RequestModification_Click(object sender, EventArgs e)
		{

			// Request a modification for this field
			if (sender == butModifyName)
				this.RequestModification(Definitions.eDataField.Contact_Name, 0, null);
			else if (sender == butModifyNotes)
				this.RequestModification(Definitions.eDataField.Contact_Notes, 0, null);
#if DEBUG
			else
				Debug.Assert(false);
#endif // #if DEBUG

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void RequestModification(Definitions.eDataField contactField, int iFieldId, string strFieldId)
		{

			// Show popup window
			RequestModificationWnd wnd = new RequestModificationWnd(contactField);
			wnd.Show();

		}

		public void Set(ContactTransport contact, ObservableCollection<BankAccountTransport> bankAccounts )
		{

			// Declaration of local variables
			int i1 = 0;

			// Name
			tbName.Text = TransportSupport.ComposeFullContactName(contact);

			// Notes
			if (string.IsNullOrEmpty(contact.Notes))
			{
				tbNotesLabel.Visibility = System.Windows.Visibility.Collapsed;
			}
			else
			{
				tbNotesLabel.Visibility = System.Windows.Visibility.Visible;
				tbNotes.Text = contact.Notes;
			}
			tbNotes.Visibility = tbNotesLabel.Visibility;
			butModifyNotes.Visibility = tbNotesLabel.Visibility;

			// Insert points
			int iExistingPointRows = Grid.GetRow(tabAddress) - PointInsertRowIndex;
			while (iExistingPointRows < contact.PointList.Count)
			{

				// Calc row index
				int iRowIndex = PointInsertRowIndex + i1;

				// Shift controls down
				LayoutRoot.ShiftRowsDown(iRowIndex, 1);

				// Add row definition
				RowDefinition row = new RowDefinition();
				row.Height = new GridLength(0, GridUnitType.Auto);
				LayoutRoot.RowDefinitions.Add(row);

				// Add point controls
				TextBlock tbLabel = new TextBlock();
				tbLabel.Style = this.Resources["ExLabelStyle"] as Style;
				LayoutRoot.Children.Add(tbLabel);
				Grid.SetColumn(tbLabel, 0);
				Grid.SetRow(tbLabel, iRowIndex);

				ReadOnlyContactPointCtrl ctrl = new ReadOnlyContactPointCtrl();
				LayoutRoot.Children.Add(ctrl);
				Grid.SetColumn(ctrl, 1);
				Grid.SetRow(ctrl, iRowIndex);

				// Move on
				iExistingPointRows++;

			}
			if (iExistingPointRows > contact.PointList.Count)
				LayoutRoot.DeleteRows(PointInsertRowIndex, iExistingPointRows - contact.PointList.Count);
			for (i1 = 0; i1 < contact.PointList.Count; i1++)
			{

				// Get the point
				ContactPointTransport cpt = contact.PointList[i1];

				// Calc row index
				int iRowIndex = PointInsertRowIndex + i1;

				// Set control
				TextBlock tbLabel = LayoutRoot.FindCellContent(0, iRowIndex, typeof(TextBlock)) as TextBlock;
#if DEBUG
				Debug.Assert(tbLabel != null);
#endif // #if DEBUG
				tbLabel.Text = string.Format("{0}:", (Definitions.eContactPointType)cpt.ContactPointType);
				ReadOnlyContactPointCtrl ctrl = LayoutRoot.FindCellContent(1, iRowIndex, typeof(ReadOnlyContactPointCtrl)) as ReadOnlyContactPointCtrl;
#if DEBUG
				Debug.Assert(ctrl != null);
#endif // #if DEBUG
				ctrl.Set(cpt);

			}

			// Addresses
			while (contact.AddressList.Count > tabAddress.Items.Count)
			{

				// Create tab header
				TabHeader tabHeader = new TabHeader();

				// Add new tab
				TabItem newTab = new TabItem();
				newTab.Background = new SolidColorBrush(Colors.Red);
				newTab.Header = tabHeader;
				tabAddress.Items.Add(newTab);

				// Add address control
				ReadOnlyContactAddressCtrl ctrl = new ReadOnlyContactAddressCtrl();
				newTab.Content = ctrl;

			}
			while (contact.AddressList.Count < tabAddress.Items.Count)
				tabAddress.Items.RemoveAt(0);
			for (i1 = 0; i1 < contact.AddressList.Count; i1++)
			{

				// Get the next address
				ContactAddressTransport address = contact.AddressList[i1];

				// Set header
				TabItem tabItem = tabAddress.Items[i1] as TabItem;
				TabHeader tabHeader = tabItem.Header as TabHeader;
				tabHeader.Label = _composeAddressLabel(address);

				// Set content			
				ReadOnlyContactAddressCtrl ctrl = tabItem.Content as ReadOnlyContactAddressCtrl;
				ctrl.Set(address);

			}

			// Bank Accounts
			while (bankAccounts.Count > tabBankAccounts.Items.Count)
			{

				// Create tab header
				TabHeader tabHeader = new TabHeader();

				// Add new tab
				TabItem newTab = new TabItem();
				newTab.Header = tabHeader;
				tabBankAccounts.Items.Add(newTab);

				// Create bank control
				ReadOnlyBankAccountCtrl ctrl = new ReadOnlyBankAccountCtrl();
				newTab.Content = ctrl;

			}
			while (bankAccounts.Count < tabBankAccounts.Items.Count)
				tabBankAccounts.Items.RemoveAt(0);
			for (i1 = 0; i1 < bankAccounts.Count; i1++)
			{

				// Get the next bank-account
				BankAccountTransport bankAccount = bankAccounts[i1];

				// Set header
				TabItem tabItem = tabBankAccounts.Items[i1] as TabItem;
				TabHeader tabHeader = tabItem.Header as TabHeader;
				tabHeader.Label = _composeBankAccountLabel(bankAccount);

				// Set content
				ReadOnlyBankAccountCtrl ctrl = tabItem.Content as ReadOnlyBankAccountCtrl;
				ctrl.Set(bankAccount);

			}

		}

		private string _composeAddressLabel(ContactAddressTransport address)
		{

			// Declare return variable
			string strFtmp;

			// Find a suitable address heading
			strFtmp = address.Postcode.Trim();
			if (strFtmp.Length == 0)
			{
				if (address.AddressLineA.Length > 0)
					strFtmp = address.AddressLineA;
				else if (address.City.Length > 0)
					strFtmp = address.City;
				else
					strFtmp = "[address]";
			}

			// Return variable
			return strFtmp;

		}

		private string _composeBankAccountLabel(BankAccountTransport bankAccount)
		{

			// Declare return variable
			string strFtmp = null;

			// Find a suitable address heading
			if (!string.IsNullOrEmpty(bankAccount.AccountNo))
				strFtmp = bankAccount.AccountNo;
			else if (!string.IsNullOrEmpty(bankAccount.AccountName))
				strFtmp = bankAccount.AccountName;
			else if (!string.IsNullOrEmpty(bankAccount.BankName))
				strFtmp = bankAccount.BankName;
			if (strFtmp == null)
				strFtmp = "[bank account]";

			// Add currency
			Money.eCurrency currency = Money.ParseCurrencyCode(bankAccount.CurrencyCode);
			strFtmp += string.Format(" ({0})", Money.CurrencyToSymbol(currency));

			// Return variable
			return strFtmp;

		}

	}

}
